#ifndef __DEVERIFYSIG_H__
#define __DEVERIFYSIG_H__

#if __linux
#include <openssl/ssl.h>
#else
#include <Windows.h>
#include <openssl/ssl.h>
#endif


class DeverifySig {

    EVP_PKEY* m_pKey;

    size_t mTotal;

    int make_REQ(X509_REQ*& x509Req)
    {
        X509_NAME* subject = NULL;
        BIO* bio = NULL;

        x509Req = X509_REQ_new();
        X509_REQ_set_pubkey(x509Req, m_pKey);

        // 设置属性
        subject = X509_REQ_get_subject_name(x509Req);
        // 国家
        X509_NAME_add_entry_by_txt(subject, SN_countryName, MBSTRING_UTF8,
            (unsigned char*)"C", -1, -1, 0);
        // 省份
        X509_NAME_add_entry_by_txt(subject, SN_stateOrProvinceName, MBSTRING_UTF8,
            (unsigned char*)"B", -1, -1, 0);
        // 城市
        X509_NAME_add_entry_by_txt(subject, SN_localityName, MBSTRING_UTF8,
            (unsigned char*)"B", -1, -1, 0);
/*
        X509_NAME_add_entry_by_txt(subject, SN_organizationName, MBSTRING_UTF8,
            (unsigned char*)"T", -1, -1, 0);

        X509_NAME_add_entry_by_txt(subject, SN_organizationalUnitName, MBSTRING_UTF8,
            (unsigned char*)"T", -1, -1, 0);

        X509_NAME_add_entry_by_txt(subject, SN_commonName, MBSTRING_UTF8,
            (unsigned char*)"T", -1, -1, 0);
*/
        X509_REQ_set_subject_name(x509Req, subject);

#if 0
        // 可视化输出
        bio = BIO_new_file("request.pem", "wb");
        PEM_write_bio_X509_REQ(bio, x509Req);
#endif
        if (bio)
            BIO_free(bio);

        return 0;
    }

    int sign(X509_REQ* x509Req, RSA* rsa);

public:
    DeverifySig()
        : m_pKey(NULL),
        mTotal(0)
    {
    }

    int doit()
    {
        BIGNUM* e = BN_new();
        BN_set_word(e, 65537);
        RSA* rsa = RSA_new();
        int tid = pthread_self();
        int ret = RSA_generate_key_ex(rsa, 1024, e, NULL);
        if (rsa == NULL) {
            printf("RSA_generate_key_ex failed \n");
            return -1;
        }

#if 0
        BIO * print_out=BIO_new(BIO_s_file());
        BIO_set_fp(print_out,stdout,BIO_NOCLOSE);
        RSA_print(print_out, rsa, 0);

        /* 提取私钥 */
        FILE *filename = NULL;
        filename = fopen("privateKey.pem", "wb");
        PEM_write_RSAPrivateKey(filename, rsa, NULL, NULL, 0, NULL, NULL);
        fclose(filename);

        /* 提取公钥 */
        FILE *publicKey = NULL;
        publicKey = fopen("publicKey.pem", "wb");
        PEM_write_RSAPublicKey(publicKey, rsa);
        fclose(publicKey);
#endif

        X509_REQ* req = NULL;
        m_pKey = EVP_PKEY_new();
        EVP_PKEY_set1_RSA(m_pKey, rsa);
        int i = make_REQ(req);

        while (1) {
            i = sign(req, rsa);
            if(mTotal++ % 10000 == 9999){
                printf("\r\033[K");
                printf("tid %d:%d", tid, mTotal);
                fflush(stdout);
            }
        }
        if (req) {
            X509_REQ_free(req);
        }
        if (m_pKey) {
            EVP_PKEY_free(m_pKey);
        }
    }
};

void tsthash();

#endif